**
** PREPARATION
**
set printcolor gs1, permanently
foreach s in 3 5 7 {
	  
* MATLAB to Stata

infile firm_id firm_age firm_cohort prod_id prod_quality prod_age prod_cohort quarter using INPUT/data_for_Stata_seed`s'00_f0.csv, clear

save INTERMEDIATE/data_seed`s'00_f0.dta, replace

* Total quality & total # products

use INTERMEDIATE/data_seed`s'00_f0.dta, clear
egen product=group( prod_id  firm_id )
distinct product prod_id

gen number_products=1
gen prod_quality5=prod_quality^5
gen log_q=log(prod_quality)
collapse (sum) number_products prod_quality prod_quality5 log_q1=log_q (mean) log_q prod_quality5v=prod_quality5, by(quarter)
gen Q=prod_quality5^(1/5)
gen log_Q=log(Q)
keep Q log_q log_Q quarter
save INTERMEDIATE/Aggregates_seed`s'00_f0.dta, replace

}


use INTERMEDIATE/data_seed300_f0.dta, clear
merge m:1 quarter using INTERMEDIATE/Aggregates_seed300_f0.dta, nogenerate
gen seed=300
tempfile x3
save `x3', replace

use INTERMEDIATE/data_seed500_f0.dta, clear
merge m:1 quarter using INTERMEDIATE/Aggregates_seed500_f0.dta, nogenerate
gen seed=500
tempfile x5
save `x5', replace

use INTERMEDIATE/data_seed700_f0.dta, clear
merge m:1 quarter using INTERMEDIATE/Aggregates_seed700_f0.dta, nogenerate
gen seed=700
tempfile x7
save `x7', replace

use `x3', clear
append using `x5'
append using `x7'

egen product=group( prod_id firm_id seed )
egen firm=group(firm_id seed)
egen fe=group(quarter seed)
save INTERMEDIATE/data_pooled_f0.dta, replace


*****
* Figure 10 a
*****

** Baseline f=17.6

use INTERMEDIATE/data_pooled.dta, clear

* DETERMINE AGE
*Defines cohort/entryTime, exitTime, age, and MaxAge
bysort product: egen x=min(quarter)
gen lcensored=(x==1)
gen cohort=x 
bysort product: egen y=max(quarter)
qui su y
gen rcensored=(y==`r(max)') 
gen exitTime=y 
gen age=quarter-cohort 
bysort product: egen maxAge=max(age) 
bysort product: egen totalobs=count(quarter)

* Type of observation 
gen type="NA"
replace type="entry"      if age==0 & age!=maxAge  
replace type="continues"  if age>0  & age<maxAge  
replace type="exit"       if age==maxAge & age!=0 
replace type="entry/exit" if age==maxAge & age==0 
gen censored="NA"  // NEW & CORRECTED
replace censored="left censored"   if lcensored==1   & rcensored==0
replace censored="right censored"  if rcensored==1 & lcensored==0
replace censored="left censored & right censored" if lcensored==1  & rcensored==1   
replace censored="not censored" if lcensored==0  & rcensored==0
tab type censored, m

gen flag_longitudinal="NA"
replace flag_longitudinal="Complete" if totalobs==y-x+1
replace flag_longitudinal="Incomplete" if totalobs<y-x+1

gen log_quality=log(prod_quality)
gen log_sales=5*log(prod_quality)
gen log_salesA=5*log(prod_quality)-5*log_q+(2-6)*log_Q
qui tab cohort, gen(cohort_d)

gen sample=0
replace sample=1 if (cohort>=2 & cohort<=23) & age<=16 & age>0 & maxAge>16 & flag_longitudinal=="Complete"
foreach num of numlist 4/23 {  // 19
gen cohort_nd`num'=cohort_d`num'-((`num'-1)*cohort_d3-(`num'-2)*cohort_d2)
}
replace age=17 if sample==0 | age>16

areg log_sales i.age cohort_nd* if sample==1, absorb(fe)
matrix results=[e(b)',vecdiag(e(V))']
svmat results
rename results1 coef
rename results2 se
drop cohort_nd* 

keep coef se
drop if coef == .
gen age=_n
drop if age>16
gen coef_u = coef + 1.96*sqrt(se)
gen coef_l = coef - 1.96*sqrt(se)

gen coef_c=coef/2.486 // NEEDS MANUAL ADJUSTMENT -- change with calibrated value
gen coef_bs=coef-coef_c

save INTERMEDIATE/ProductLifeCycle.dta, replace

** Counterfactual f=0

use INTERMEDIATE/data_pooled_f0.dta, clear


* DETERMINE AGE
*Defines cohort/entryTime, exitTime, age, and MaxAge
bysort product: egen x=min(quarter)
gen lcensored=(x==1)
gen cohort=x 
bysort product: egen y=max(quarter)
qui su y
gen rcensored=(y==`r(max)') 
gen exitTime=y 
gen age=quarter-cohort 
bysort product: egen maxAge=max(age) 
bysort product: egen totalobs=count(quarter)

* Type of observation 
gen type="NA"
replace type="entry"      if age==0 & age!=maxAge  
replace type="continues"  if age>0  & age<maxAge  
replace type="exit"       if age==maxAge & age!=0 
replace type="entry/exit" if age==maxAge & age==0 
gen censored="NA"  // NEW & CORRECTED
replace censored="left censored"   if lcensored==1   & rcensored==0
replace censored="right censored"  if rcensored==1 & lcensored==0
replace censored="left censored & right censored" if lcensored==1  & rcensored==1   
replace censored="not censored" if lcensored==0  & rcensored==0
tab type censored, m

gen flag_longitudinal="NA"
replace flag_longitudinal="Complete" if totalobs==y-x+1
replace flag_longitudinal="Incomplete" if totalobs<y-x+1

gen log_quality=log(prod_quality)
gen log_sales=5*log(prod_quality)
gen log_salesA=5*log(prod_quality)-5*log_q+(2-6)*log_Q
qui tab cohort, gen(cohort_d)

gen sample=0
replace sample=1 if (cohort>=2 & cohort<=23) & age<=16 & age>0 & maxAge>16 & flag_longitudinal=="Complete"
foreach num of numlist 4/23 {  // 19
gen cohort_nd`num'=cohort_d`num'-((`num'-1)*cohort_d3-(`num'-2)*cohort_d2)
}
replace age=17 if sample==0 | age>16

areg log_sales i.age cohort_nd* if sample==1, absorb(fe)
matrix results=[e(b)',vecdiag(e(V))']
svmat results
rename results1 coef
rename results2 se
drop cohort_nd* 

keep coef se
drop if coef == .
gen age=_n
drop if age>16
gen coef_u = coef + 1.96*sqrt(se)
gen coef_l = coef - 1.96*sqrt(se)

gen coef_c=coef/2.486 // NEEDS MANUAL ADJUSTMENT -- change with calibrated value
gen coef_bs=coef-coef_c

save INTERMEDIATE/ProductLifeCycle_f0.dta, replace



use INTERMEDIATE/ProductLifeCycle.dta, replace
gen baseline=1
append using INTERMEDIATE/ProductLifeCycle_f0.dta
replace baseline=0 if baseline==.

twoway ///
(line coef age if age<=16 & baseline==1, color(midblue) lwidth(vthick)) ///
(line coef age if age<=16 & baseline==0, color(maroon) lwidth(vthick) lpattern(dash)) ///
, ytitle("Estimated Sales (log scale)") xtitle("Age (quarters)") xlabel(4(4)16) ///
title("") ylabel(-0.3(0.1)0) ///
legend(off)  ysize(4.7) ///
text(-.12 5.5 "Baseline f=17.6")  text(-.178 14.4 "Counterfactual f=0") ///
graphregion(color(white)) plotregion(fcolor(white)) legend(region(lcolor(white))) note("")
graph export OUTPUT/Figure10a.eps, replace




